package com.my.util;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class DateUtil {

    /**
     * date转String
     * format是转换格式 如"yyyy-MM-dd"
     * @param date
     * @param format
     * @return
     */
    public static String dateFormatYYYYMMDD(Date date, String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(date);
    }

    /**
     * string转date类型
     * format是strDate的格式;strDate是 2018-02-26, format就是"yyyy-MM-dd"
     * @param strDate
     * @param format
     * @return
     */
    public static Date strToDate(String strDate, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    /**
     * 获取某个日期之前或者之后的某个日期
     * @param nowdate
     * @param delay 间隔天数
     * @param formatter
     * @return
     */
    public static String getNextDay(String nowdate, String delay, String formatter) {
        try {
            SimpleDateFormat format = new SimpleDateFormat(formatter);
            Date d = strToDate(nowdate, formatter);
            long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24 * 60 * 60;
            d.setTime(myTime * 1000);
            return format.format(d);
        } catch (Exception e) {
            return "";
        }
    }

    /**
     *获取date增加offset时间
     * @param offset
     * @param date
     * @return Date
     * */
    public static Date getOffSetDateTime(Integer offset,Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, offset);
        return calendar.getTime();
    }

    /**
     *获取date增加offset时间
     * @param offset
     * @param date
     * @return String yyyy-MM-dd
     * */
    public static String getOffSetStrTime(Integer offset,Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, offset);
        String offsetDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
        return offsetDate;
    }

    public static int getDatesDiffer(String fromDate, Date toDate) {
        fromDate = fromDate.substring(0, 10);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String toDateStr = sdf.format(toDate);
        toDateStr = toDateStr.substring(0, 10);
        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd");

        long from = 0L;
        long to = 0L;
        int days = 0;
        try {
            from = simpleFormat.parse(fromDate).getTime();
            to = simpleFormat.parse(toDateStr).getTime();
            days = (int) ((to - from)/(1000 * 60 * 60 * 24));
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
        return days;
    }

    /**
     *获取day所属当天的时间范围
     * */
    public static Map<Integer, Date> getDateTimeRange(Date day) {
        //获取当天开始结束时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(day);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        Date startDate = calendar.getTime();

        calendar.add(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.SECOND, -1);

        Date endDate = calendar.getTime();

        Map<Integer, Date> result = new HashMap<Integer, Date>();
        result.put(0, startDate);
        result.put(1, endDate);
        return result;
    }


    public static boolean isGapBiggerThanDays(Date date1, Date date2, Integer days){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date1);
        calendar.add(Calendar.DATE, days);
        if(calendar.getTime().getTime() < date2.getTime()){
            return true;
        }
        return false;
    }



    public static void main(String[] args) throws Exception{

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//        String deadline = "2020-09-07 23:59:59";
//        boolean res = DateUtil.isGapBiggerThanDays(new Date(), dateFormat.parse(deadline), 5);
//
//        System.out.println("res=====" + res);
//
//        Map<Integer, Date> dateMap = DateUtil.getDateTimeRange(DateUtil.getOffSetDateTime(-1, Calendar.getInstance().getTime()));
//        System.out.println(dateMap);


        String startDateStr = "2020-09-28 14:46:16";
        String endDateStr = "2020-09-28 15:00:28";

        Date startTime = dateFormat.parse(startDateStr);

        Date endTime = dateFormat.parse(endDateStr);

        System.out.println(endTime.getTime() - startTime.getTime());

    }
}
